package hardy.etudejpa.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import hardy.etudejpa.model.UserAuthEntity;
import hardy.etudejpa.service.VoTestService;

@RestController
@RequestMapping("/user")
public class UserAuthController {
	@Inject
	private VoTestService test;
	@PostMapping("/save")
    public String save(String phone){
		test.saveUser(phone);
        return "success";
    }
	  
	    @PostMapping("/select")
	    public String select(UserAuthEntity ua){
	        return JSONObject.toJSONString(test.findUser(ua));
	    }
	    
	    @PostMapping("/batch")
	    public String batch(String[] phone){
	    	test.batchSaveUser(phone);
	    	return "success";
	    }
	    
	    @PostMapping("/updateEmail")
	    public String updateEmail(UserAuthEntity ua){
	    	ua.setAddDate(new Date());
	    	test.updateEmail(ua);
	    	return "success";
	    }
	    
	    @PostMapping("/updatepw")
	    public String updateEmail2(String id,String email,String phone,String password){
	    	UserAuthEntity ua = new UserAuthEntity();
	    	ua.setUserId(id);
	    	ua.setPhone(phone);
	    	ua.setEmail(email);
	    	ua.setAddDate(new Date());
	    	ua.setPassword(password);
	    	test.updatePassword(ua);
	    	return "success";
	    }
	    
	    @PostMapping("/del1")
	    public String del1(UserAuthEntity ua){
	    	test.delete(ua);
	    	return "success";
	    }
	    
	    @PostMapping("/del2")
	    public String del2(String[] ids,String[] phones){
	    	List<UserAuthEntity> list = new ArrayList<UserAuthEntity>();
	    	for(int i=0;i<ids.length;i++) {
	    		UserAuthEntity ua = new UserAuthEntity();
		    	ua.setUserId(ids[i]);
		    	ua.setPhone(phones[i]);
		    	list.add(ua);
	    	}
	    	test.deleteF(list);
	    	return "success";
	    }
	    
	   
}
